package com.ruoyi.cityVote.controller;

import com.ruoyi.cityVote.domain.CityVote;
import com.ruoyi.cityVote.service.ICityVoteService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.other.mapper.CityConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 城市投票Controller
 *
 * @author ruoyi
 * @date 2025-04-16
 */
@RestController
@RequestMapping("/cityVote/cityVote")
public class CityVoteController extends BaseController {
    @Autowired
    private ICityVoteService cityVoteService;
    @Resource
    private CityConfigMapper cityConfigMapper;

    /**
     * 查询城市投票列表
     */
    @PreAuthorize("@ss.hasPermi('cityVote:cityVote:list')")
    @GetMapping("/list")
    public TableDataInfo list(CityVote cityVote) {
        startPage();
        List<CityVote> list = cityVoteService.selectCityVoteList(cityVote);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (CityVote vote : list) {
                Integer count = cityConfigMapper.selectOne(vote.getCityName());
                if (count > 0) {
                    vote.setOpenStatus(1);
                    cityVoteService.updateCityVote(vote);
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出城市投票列表
     */
    @PreAuthorize("@ss.hasPermi('cityVote:cityVote:export')")
    @Log(title = "城市投票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CityVote cityVote) {
        List<CityVote> list = cityVoteService.selectCityVoteList(cityVote);
        ExcelUtil<CityVote> util = new ExcelUtil<CityVote>(CityVote.class);
        util.exportExcel(response, list, "城市投票数据");
    }

    /**
     * 获取城市投票详细信息
     */
    @PreAuthorize("@ss.hasPermi('cityVote:cityVote:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cityVoteService.selectCityVoteById(id));
    }

    /**
     * 新增城市投票
     */
    @PreAuthorize("@ss.hasPermi('cityVote:cityVote:add')")
    @Log(title = "城市投票", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CityVote cityVote) {
        return toAjax(cityVoteService.insertCityVote(cityVote));
    }

    /**
     * 修改城市投票
     */
    @PreAuthorize("@ss.hasPermi('cityVote:cityVote:edit')")
    @Log(title = "城市投票", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CityVote cityVote) {
        return toAjax(cityVoteService.updateCityVote(cityVote));
    }

    /**
     * 删除城市投票
     */
    @PreAuthorize("@ss.hasPermi('cityVote:cityVote:remove')")
    @Log(title = "城市投票", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cityVoteService.deleteCityVoteByIds(ids));
    }
}
